package home_8_25.dao.impl;

import com.neu.Dao.BaseDao;
import home_8_25.dao.FoodDao;

import java.util.List;
import java.util.Map;

public class FoodDaoImpl extends BaseDao implements FoodDao {
    @Override
    public List<Map<String, Object>> finAll(String id) {

        List<Map<String,Object>> list = null;
        String sql = "select fname,fexplain,fprice,fbusid,\n" +
                "case\n" +
                "when fstatus=1 then '在售'\n" +
                "else '售空'\n" +
                "end food_status\n" +
                "from tfood";
        list = super.executeQuery(sql,id);
        return list;
    }

    @Override
    public List<Map<String, Object>> findByName(String name) {
        List<Map<String,Object>> list = null;
        String sql = "select fname,fexplain,fprice,fbusid,\n" +
                " case\n" +
                " when fstatus=1 then '在售'\n" +
                " else '售空'\n" +
                " end food_status\n" +
                " from tfood \n" +
                " where  fname like ?";
        list = super.executeQuery(sql,"%"+name+"%");
        return list;
    }

    @Override
    public int insertFood(String name, String price, String info, String id) {
        int row = 0;
        String sql = "insert into tfood (fname,fprice,fexplain，fbusid)\n" +
                " " +
                " values (?,?,?,?)";
        row = super.executeUpdate(sql,name,price,info,id);

        return row;
    }

    @Override
    public Map<String, Object> findFoodById(String foodId, String id) {
        Map<String, Object> map = null;


        String sql = "select * from tfood \n" +
                "where fid = ? and fbusid = ?\n" +
                "and fstatus = 1";
        //已删除的商品不再被修改；
        List<Map<String,Object>> list = super.executeQuery(sql,foodId,id);
        if (list.size()>0){
            map = list.get(0);

        }



        return map;
    }

    @Override
    public int editFood(String name, String price, String info, String foodId) {
        int row = 0;

        String sql = "update tfood set fname = ? ,fprice = ? ,fexplain = ? " +
                " where fid = ?";
        row = super.executeUpdate(sql,name,price,info,foodId);

        return row;
    }




}












































